using System;
using System.Windows.Controls;
using System.Windows;

namespace LifeV2.Skins
{
    public class DynamicGrid : Grid
    {

        public static readonly DependencyProperty NumColumnsProperty =
            DependencyProperty.Register("NumColumns", typeof(Int32), typeof(DynamicGrid));

        public int NumColumns
        {
            get { return (int)GetValue(NumColumnsProperty); }
            set { SetValue(NumColumnsProperty, value); }
        }


        public static readonly DependencyProperty NumRowsProperty =
            DependencyProperty.Register("NumRows", typeof(int), typeof(DynamicGrid));

        public int NumRows
        {
            get { return (int)GetValue(NumRowsProperty); }
            set { SetValue(NumRowsProperty, value); }
        }


        private void RecreateGridCells()
        {
            int numRows = NumRows;
            int currentNumRows = RowDefinitions.Count;

            while (numRows > currentNumRows)
            {
                RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
                currentNumRows++;
            }

            while (numRows < currentNumRows)
            {
                currentNumRows--;
                RowDefinitions.RemoveAt(currentNumRows);
            }

            int numCols = NumColumns;
            int currentNumCols = ColumnDefinitions.Count;

            while (numCols > currentNumCols)
            {
                ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                currentNumCols++;
            }

            while (numCols < currentNumCols)
            {
                currentNumCols--;
                ColumnDefinitions.RemoveAt(currentNumCols);
            }

            UpdateLayout();

        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            RecreateGridCells();
        }

    }
}
